import clone from "@/lib/clone";

const localStorageKeyName = 'recordList';
const data: RecordItem[]|undefined=undefined

const recordStore= {
    recordList: [] as RecordItem[],
    fetchRecords() {
        this.recordList = JSON.parse(window.localStorage.getItem(localStorageKeyName) || '[]') as RecordItem[];  //用于保存每天的数据
        return this.recordList
        //注意写上返回值的类型 不然就是any类型----so 你知道怎么样知道一个函数返回值的类型吗？

    },
    saveRecords(){
        window.localStorage.setItem(localStorageKeyName, JSON.stringify(this.recordList));

    },
    createRecord(record: RecordItem){
        const record2: RecordItem = clone(record)
        record2.createdAt = new Date().toISOString()
        //可选链语法 ES2019 stage3阶段
        this.recordList && this.recordList.push(record2);
        // this.recordList.push?.(record2);
        //最新版语法 新到如果你的开发工具 typescript 版本不是最新都可能报错
        recordStore.saveRecords();
    },
};

recordStore.fetchRecords();

export default recordStore;
